<script>
	import store from '@/store/index.js'
	export default {
		onShow() {
			this.scrollTop = 9999999;
			this.connectWebsocket()
		},
		onHide() {
			// this.$socket.closeSocket();
		},
		destroyed() {
			// this.$socket.closeSocket();
		},
		// 在 App.vue 的 onLaunch 中
		onLaunch() {
			let token = uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			if (!token) {
				uni.navigateTo({
					url: '/packages/login/login'
				})
			}
			this.getloaction()

			// uni.switchTab({
			// 	url: '/pages/map/map'
			// });
		},
		methods: {
			setAllMsgNumFun(num) {
				store.commit('setAllMsgNum', num)
			},
			setUnQuantityFun(obj) {
				store.commit('setUnQuantity', obj)
			},
			screenMsg(msg) {
				this.setAllMsgNumFun(msg.allUnQuantity)
				this.setUnQuantityFun(msg.unQuantity)
			},
			connectWebsocket() {
				const token = uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
				this.$socket.token = token;
				this.$socket.connectSocketInit(() => {
					this.$socket.getMessage(this.screenMsg);
				});
			},
			// 登录
			// async login() {
			// 	let that = this
			// 	uni.login({
			// 		"provider": "weixin",
			// 		"onlyAuthorize": true, // 微信登录仅请求授权认证
			// 		async success(event) {
			// 			// 客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
			// 			let res = await that.$u.api.login({
			// 				code: event.code
			// 			});
			// 			if (res.code == 200) {
			// 				uni.showToast({
			// 					icon: 'fail',
			// 					title: '登录成功'
			// 				})
			// 			}
			// 			uni.setStorageSync('token', res.data)
			// 		},
			// 		fail: function(err) {
			// 			console.log(err);
			// 		}
			// 	})
			// },

			// 获取经纬度
			async getloaction() {
				let that = this
				let res1 = await that.$u.api.getlocateCitye()
				let city = res1.data.abbrName ? res1.data.abbrName : ''
				uni.setStorageSync('city', city)
				uni.setStorageSync('id', res1.data.id)
				// uni.authorize({
				// 	scope: 'scope.userLocation',
				// 	success() {
				// 		uni.getLocation({
				// 			type: 'wgs84',
				// 			async success(res) {
				// 				let location = res.latitude + ',' + res.longitude
				// 				let res1 = await that.$u.api.getlocateCitye({
				// 					location: location
				// 				})
				// 				let city = res1.data.areaName
				// 				uni.setStorageSync('city', city)
				// 				uni.setStorageSync('id', res1.data.id)
				// 			}
				// 		});
				// 	},
				// 	async fail() {
				// 		console.log('hhhh');
				// 	}
				// });
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni.scss';

	page {
		background-color: rgb(255, 255, 255);
		// padding-bottom: 20rpx;
		// box-sizing: border-box;
	}
</style>